package cn.kylinos.springportal.access.controller;

import cn.kylinos.springportal.access.service.AccesslogService;
import cn.kylinos.springportal.common.RestResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

/**
 * Accesslog controller
 * Note that log only has retrieve API
 * @author lichenghong
 */
@RestController
@RequestMapping("/accesslog")
public class AccesslogController {
    @Autowired
    private AccesslogService accesslogService;

    @GetMapping("")
    public RestResponse index(@RequestParam(value = "page", defaultValue = "0") Integer page,
                              @RequestParam(value = "pageSize", defaultValue = "0") Integer pageSize) {
        if (page == 0 && pageSize == 0) {
            return RestResponse.createSuccessResponse(accesslogService.findAll());
        } else {
            Pageable pageable = PageRequest.of(page, pageSize);
            return RestResponse.createSuccessResponse(accesslogService.findAll(pageable));
        }
    }

    @GetMapping("/{logId}")
    public RestResponse get(@PathVariable Integer logId) {
        return RestResponse.createSuccessResponse(accesslogService.findById(logId));
    }
}
